package cn.btks.databasehandler.handle.controller;

import cn.btks.commonserver.result.MenuSourceEnum;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.SystemLogEnum;
import cn.btks.commonserver.valid.SystemLog;
import cn.btks.databasehandler.handle.service.BackUpResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

/**
 * @Author lijun
 * @Date 2020/12/28 20:56
 */
@RestController
@RequestMapping("api/v1.0/backUpConfig")
@Api(tags = "备份配置模块")
@Validated
public class BackUpResultController {

    @Autowired
    BackUpResultService backUpResultService;

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_DATABASE)
    @GetMapping("/result/findPages")
    @ApiOperation("分页查询备份配置定时执行结果信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "configName", value = "配置名称（模糊查询）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "configMode", value = "配置类型（0：按照库进行备份 1：按照表进行备份）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "configCycle", value = "备份周期（0：年/次 1：半年/次 2：月/次 9：其他）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPagesByConfigNameAndConfigModeAndConfigCycle(Integer pageNumber, Integer pageSize, String configName, Integer configMode, Integer configCycle) {
        return backUpResultService.findPagesByConfigNameAndConfigModeAndConfigCycle(pageNumber, pageSize, configName, configMode, configCycle);
    }

    @SystemLog(type = SystemLogEnum.DELETE_LOG,moduleMessage = MenuSourceEnum.MENU_DATABASE)
    @DeleteMapping("/result/delete/{id}")
    @ApiOperation("根据ID删除备份结果信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "备份结果ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState delete(@PathVariable("id") Long id) {
        return backUpResultService.delete(id);
    }

    @SystemLog(type = SystemLogEnum.RESUME_LOG,moduleMessage = MenuSourceEnum.MENU_DATABASE)
    @GetMapping("/result/resume/{id}")
    @ApiOperation("备份恢复（仅对备份成功的进行恢复操作）")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "备份配置执行结果ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState resume(@PathVariable("id") Long id) throws SQLException {
        return backUpResultService.resumeConfig(id);
    }
}
